<?php
declare (strict_types = 1);

namespace app;

use think\App;
use think\exception\ValidateException;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {}

    /**
     * 验证数据
     * @access protected
     * @param  array        $data     数据
     * @param  string|array $validate 验证器名或者验证规则数组
     * @param  array        $message  提示信息
     * @param  bool         $batch    是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                [$validate, $scene] = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v     = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);

        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        return $v->failException(true)->check($data);
    }

    //sign
    public function getSignature($params,$key): string
    {
        ksort($params);//排序
        $params =  urldecode(http_build_query($params));//转换成key=value
        $params .= "&key={$key}";
        return strtoupper(md5($params));
    }

    // 加密
    public function encrypt(string $data,$key,$iv): string
    {
        $encrypted = openssl_encrypt($data, "AES-128-CBC", $key, true, $iv);
        return base64_encode($encrypted);
    }

    // 解密
    public function decrypt(string $data,$key,$iv): string
    {
        $data = base64_encode($data);
        $encrypted = openssl_decrypt($data, "AES-128-CBC", $key, true, $iv);
        return $encrypted;
    }

    public function success($data=[],$msg='成功',$code=200){
        
        return json(['data'=>$data,'msg'=>$msg,'code'=>$code]);
    }

    public function error($data=[],$msg='失败',$error_code='0001',$code=0){
        
        return json(['data'=>$data,'msg'=>$msg,'error_code'=>$error_code,'code'=>$code]);
    }

    public function curlPost($url,$param){

        $curl = curl_init();
        $param_json = json_encode($param);
        curl_setopt_array($curl, array(
           CURLOPT_URL => $url,
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_ENCODING => '',
           CURLOPT_MAXREDIRS => 10,
           CURLOPT_TIMEOUT => 0,
           CURLOPT_FOLLOWLOCATION => true,
           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
           CURLOPT_CUSTOMREQUEST => 'POST',
           CURLOPT_POSTFIELDS =>$param_json,
           CURLOPT_HTTPHEADER => array(
              'User-Agent: Apifox/1.0.0 (https://apifox.com)',
              'Content-Type: application/json'
           ),
        ));
        
        $response = curl_exec($curl);
        
        return json_decode($response,true);
    }
}
